package com.piece.admin.support;

import com.piece.admin.model.TbSysMenu;
import com.piece.core.framework.enums.StatusType;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.util.string.StringUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@ApiModel("系统路由")
public class Route implements Serializable {

    @ApiModelProperty("ID")
    private String id;

    @ApiModelProperty("名称")
    private String name;

    @ApiModelProperty("路径")
    private String path;

    @ApiModelProperty("重定向地址")
    private String redirect;

    @ApiModelProperty("VUE组件名称")
    private String component;

    @ApiModelProperty("跳转方式")
    private String target;

    @ApiModelProperty("是否隐藏")
    private boolean hidden;

    @ApiModelProperty("是否始终展示")
    private boolean alwaysShow;

    @ApiModelProperty("元数据")
    private Meta meta;

    @ApiModelProperty("子路由")
    private List<Route> children;

    public Route() {
        super();
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel("路由元数据")
    public static class Meta implements Serializable {
        @ApiModelProperty("ID")
        private String id;
        @ApiModelProperty("标题")
        private String title;
        @ApiModelProperty("图标")
        private String icon;
        @ApiModelProperty("是否缓存")
        private boolean noCache;
        @ApiModelProperty("前缀")
        private boolean affix;
        @ApiModelProperty("是否加入面包屑")
        private boolean breadcrumb;
        @ApiModelProperty("激活路由")
        private String activeMenu;
        @ApiModelProperty("跳转方式")
        private String target;
        @ApiModelProperty("访问权限")
        private String permission;
    }

    public List<Route> convert(List<TbSysMenu> subMenus) {
        return this.convert(null, subMenus).getChildren();
    }

    private Route convert(TbSysMenu menu, List<TbSysMenu> subMenus) {
        Route parentRoute;
        if (null != menu) {
            parentRoute = convert(new Route(), menu);
            if (StringUtil.isEmpty(parentRoute.getComponent())) {
                return null;
            }
        } else {
            parentRoute = new Route();
        }

        List<Route> childRoutes = new ArrayList<>();
        Route childRoute;
        for (TbSysMenu subMenu : subMenus) {
            childRoute = convert(new Route(), subMenu);
            if (StringUtil.isEmpty(childRoute.getComponent())) {
                continue;
            }
            if (null != subMenu.getChildren() && subMenu.getChildren().size() > 0) {
                childRoute = convert(subMenu, subMenu.getChildren());
                if (null == childRoute) {
                    continue;
                }
                childRoute.getMeta().setActiveMenu(subMenu.getPath());
            }
            childRoutes.add(childRoute);
        }
        parentRoute.setChildren(childRoutes);
        return parentRoute;
    }

    private Route convert(Route route, TbSysMenu menu) {
        route.setId(menu.getMenuId() + "");
        route.setName(menu.getMenuName());
        route.setPath(Convert.toStr(menu.getPath(), null));
        route.setRedirect(Convert.toStr(menu.getRedirect(), null));
        route.setComponent(menu.getComponent());
        route.setHidden(StatusType.HIDDEN.getCode().equals(menu.getVisible()));
        route.setAlwaysShow(false);
        Meta meta = new Meta();
        meta.setId(menu.getMenuId() + "");
        meta.setTitle(menu.getMenuName());
        meta.setIcon(menu.getIcon());
        meta.setNoCache(false);
        meta.setAffix(false);
        meta.setBreadcrumb(true);
        meta.setTarget(menu.getTarget());
        meta.setPermission(menu.getPerms());
        route.setMeta(meta);
        return route;
    }
}
